package top.vanchor.myblog2back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import top.vanchor.myblog2back.entity.BlogChat;
import top.vanchor.myblog2back.mapper.BlogChatMapper;
import top.vanchor.myblog2back.service.IBlogChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-01-11
 */
@Service
public class BlogChatServiceImpl extends ServiceImpl<BlogChatMapper, BlogChat> implements IBlogChatService {

    @Resource
    private BlogChatMapper blogChatMapper;

    @Override
    public int addMsg(BlogChat blogChat) {
        return blogChatMapper.insert(blogChat);
    }

    @Override
    public List<BlogChat> getChatsByRoomId(String sid) {
        Integer roomId = Integer.parseInt(sid);
        QueryWrapper<BlogChat> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id",roomId);
        wrapper.orderBy(true,false,"chat_id");
        wrapper.last("limit 5");
        List<BlogChat> list = blogChatMapper.selectList(wrapper);
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<BlogChat> getChatsMoreByRoomIdAndlastChatId(String sid, Integer chatId) {
        Integer roomId = Integer.parseInt(sid);
        QueryWrapper<BlogChat> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id",roomId);
        wrapper.orderBy(true,false,"chat_id");
        wrapper.lt("chat_id",chatId);
        wrapper.last("limit 10");
        return blogChatMapper.selectList(wrapper);
    }
}
